<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.userName" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column label="头像(查看大图)" align="center" width="140">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.avatar"
                            :preview-src-list="[scope.row.avatar]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="userName" label="用户名"></el-table-column>
                <el-table-column label="用户角色" width="100">
                    <template slot-scope="scope">
                        {{scope.row.roleId == 1?'管理员':'微信用户'}}
                    </template>
                </el-table-column>
                <el-table-column prop="mobileNo" label="手机号" width="120"></el-table-column>
                <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
                <el-table-column prop="createdTime" label="注册时间"></el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-lx-lock"
                            class="orange mid"
                            @click="handlePsd(scope.$index, scope.row)">密码</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            class="mid"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <!-- <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize" 
                    :total="tableData.length"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="修改角色" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="角色">
                    <el-radio v-model="roleId" label="1">管理员</el-radio>
                    <el-radio v-model="roleId" label="2">微信用户</el-radio>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="修改密码" :visible.sync="psdVisible" width="30%">
            <el-form ref="form" :model="psdForm" label-width="70px">
                <el-form-item label="密码">
                    <el-input v-model="psdForm.password"></el-input>
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input v-model="psdForm.confirmPwd"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="psdVisible = false">取 消</el-button>
                <el-button type="primary" @click="savePsd">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                userName: '',
                pageNum: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            psdVisible:false,
            psdForm:{},
            editForm:{},
            roleId:'',
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$http.postHead('hgz/user/queryListWithPage',this.query).then(res =>{
                if(res.status){
                   this.tableData = res.data.list;
                }
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageNum', 1);
            this.getData();
        },
        handlePsd(index, row){
            this.idx = index;
            this.psdForm = {};
            this.psdVisible = true;
        },
        savePsd(){
            var param ={
                id: localStorage.getItem('user_id'),
                password:this.psdForm.password,
                confirmPwd:this.psdForm.confirmPwd
            }
            if(this.psdForm.password == ''){
                 this.$message.error('请输入密码');   
                return;
            }
            if(this.psdForm.confirmPwd = ''){
                 this.$message.error('确认密码');
                return;
            }
            this.$http.postHead('hgz/user/modifyPwd',param).then(res =>{
                if(res.status){
                   this.$message.success("修改成功");
                   this.psdVisible = false;
                }else{
                   this.$message.error(res.message);
                }
            })
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            if(row.roleId != 1){
                this.roleId = '2';
            }else{
                this.roleId = '1';
            }
            this.editForm = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            console.log(this.editForm);
            var roleId = 2;
            if(this.roleId == '1'){
                roleId = 1;
            }else{
                roleId = 2;
            }
            var param = {
                id:this.editForm.id,
                roleId:roleId,
            }
            this.$http.postHead('hgz/user/save',param).then(res =>{
                if(res.status){
                   this.$message.success("修改成功");
                   this.editVisible = false;
                   this.getData();
                }else{
                   this.$message.error(res.message);
                }
            })

        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNum', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.orange {
    color: orange;
}
.mid{
    font-size: 16px;;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
